package com.cdgc.web.controller;

import com.cdgc.web.pojo.Mendian;
import com.cdgc.web.service.MendianService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class MendianController {
    @Autowired
    private MendianService mendianService;

    private final int PAGE_SIZE=6;
    @RequestMapping("/queryAllMendians")
    public ModelAndView queryAllMendians(@RequestParam(value = "pageNo",
            required = false,defaultValue = "1") int pageNo){

        PageHelper.startPage(pageNo,PAGE_SIZE);
        List<Mendian> mendianList = mendianService.queryAllMendians();
        ModelAndView mv = new ModelAndView();
        mv.addObject("mendianList",mendianList);
        PageInfo<Mendian> pageInfo = new PageInfo<>(mendianList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("mendian");
        return mv;
    }

    @RequestMapping("/addNewMendian")
    public String addNewMendian(Mendian mendian){
        mendianService.addNewMendian(mendian);
        return "redirect:/queryAllMendians";
    }

    //根据id查找门店
    @RequestMapping("/toUpdateMendian")
    public ModelAndView toUpdateMendian(Integer mid){
        Mendian mendian = mendianService.queryMendianById(mid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("mendian",mendian);
        mv.setViewName("UpdateMendian");
        return mv;
    }

    //修改门店
    @RequestMapping("/UpdateMendian")
    public String updateMendian(Mendian mendian){
        mendianService.updateMendian(mendian);
        return "redirect:/queryAllMendians";
    }
}
